﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

namespace Light_Calendar
{
    /// <summary>
    /// Interaction logic for EventWindow.xaml
    /// </summary>
    public partial class EditWindow : Window
    {
        private bool toSave;
        private Light_Calendar.MainWindow mainWindow;

        public CalendarEvent Event { get; set; }
        public CalendarEvent OldEvent { get; set; }
        public bool IsClosed { get; set; }

        public EditWindow(CalendarEvent e)
        {
            InitializeComponent();

            toSave = false;
            mainWindow = (Light_Calendar.MainWindow)App.Current.MainWindow;

            Event = new CalendarEvent(e);
            OldEvent = new CalendarEvent(e);
            IsClosed = false;

            date.CurrentlySelectedDate = e.Date;
            date.SelectedDateChanged += new AC.AvalonControlsLibrary.Controls.DateSelectedChangedEventHandler(date_SelectedDateChanged);

            Color[] colorSet = { Color.FromRgb(255, 255, 255), Color.FromRgb(255, 255, 204), Color.FromRgb(204, 255, 204),
                                 Color.FromRgb(204, 255, 255), Color.FromRgb(204, 204, 255), Color.FromRgb(255, 204, 255),
                                 Color.FromRgb(255, 204, 204), Color.FromRgb(204, 204, 204) };
            color.ItemsSource = colorSet;
            color.SelectedValue = e.Color;
            color.SelectionChanged += new SelectionChangedEventHandler(color_SelectionChanged);

            description.Text = e.Description;
            description.TextChanged += new TextChangedEventHandler(description_TextChanged);
            
            RepeatType[] repeatTypeSet = { RepeatType.None, RepeatType.Daily, RepeatType.Weekly,
                                           RepeatType.Monthly, RepeatType.Yearly };
            repeats.ItemsSource = repeatTypeSet;
            repeats.SelectedValue = e.Repeats;
            repeats.SelectionChanged += new SelectionChangedEventHandler(repeats_SelectionChanged);

            isForever.IsChecked = (e.Until == DateTime.MinValue);
            if ((RepeatType)repeats.SelectedValue == RepeatType.None) isForever.IsEnabled = false;
            isForever.Checked += new RoutedEventHandler(isForever_Checked);

            isUntil.IsChecked = (e.Until != DateTime.MinValue);
            if ((RepeatType)repeats.SelectedValue == RepeatType.None) isUntil.IsEnabled = false;
            isUntil.Checked += new RoutedEventHandler(isUntil_Checked);

            if (e.Until >= e.Date) until.CurrentlySelectedDate = e.Until;
            else until.CurrentlySelectedDate = e.Date;
            if ((RepeatType)repeats.SelectedValue == RepeatType.None || isForever.IsChecked.Value) until.IsEnabled = false;
            until.SelectedDateChanged += new AC.AvalonControlsLibrary.Controls.DateSelectedChangedEventHandler(until_SelectedDateChanged);

            Dictionary<int, string> reminderSet = new Dictionary<int, string>();
            reminderSet[-1] = "None";
            reminderSet[0] = "On Date";
            reminderSet[1] = "1 Day";
            reminderSet[3] = "3 Days";
            reminderSet[7] = "1 Week";
            reminderSet[14] = "2 Weeks";
            reminderSet[30] = "1 Month";
            reminder.ItemsSource = reminderSet;
            reminder.SelectedValuePath = "Key";
            reminder.DisplayMemberPath = "Value";
            reminder.SelectedValue = e.Reminder;
            reminder.SelectionChanged += new SelectionChangedEventHandler(reminder_SelectionChanged);
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            Save();
            Close();
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Save()
        {
            Event.IsNew = false;
            mainWindow.Data.Update(Event);
            new Thread(new ThreadStart(mainWindow.Data.Save)).Start();
            toSave = true;
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            mainWindow.Topmost = true;
            mainWindow.Topmost = false;
            Topmost = true;
            Topmost = false;
            description.Focus();
        }

        private void Move(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter: Save(); Close(); break;
                case Key.Escape: Close(); break;
            }
        }

        private void Window_Closing(object sender, EventArgs e)
        {
            if (!toSave)
            {
                if (Event.IsNew) mainWindow.Data.Remove(Event);
                else mainWindow.Data.Update(OldEvent);
            }
            IsClosed = true;
            mainWindow.Activate();
        }

        private void date_SelectedDateChanged(object sender, AC.AvalonControlsLibrary.Controls.DateSelectedChangedRoutedEventArgs e)
        {
            Event.Date = date.CurrentlySelectedDate;
            if (isUntil.IsChecked.Value && until.CurrentlySelectedDate < Event.Date)
                until.CurrentlySelectedDate = Event.Date;
            mainWindow.Data.Update(Event);
        }
        
        private void description_TextChanged(object sender, TextChangedEventArgs e)
        {
            Event.Description = description.Text;
            mainWindow.Data.Update(Event);
        }

        private void color_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Event.Color = (Color)color.SelectedValue;
            mainWindow.Data.Update(Event);
        }

        private void repeats_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Event.Repeats = (RepeatType)repeats.SelectedValue;
            if (Event.Repeats == RepeatType.None)
            {
                isForever.IsEnabled = false;
                isUntil.IsEnabled = false;
                until.IsEnabled = false;
            }
            else
            {
                isForever.IsEnabled = true;
                isUntil.IsEnabled = true;
                if (isUntil.IsChecked.Value) until.IsEnabled = true;
                else until.IsEnabled = false;
            }
            mainWindow.Data.Update(Event);
        }

        private void isForever_Checked(object sender, RoutedEventArgs e)
        {
            Event.Until = DateTime.MinValue;
            until.IsEnabled = false;
            mainWindow.Data.Update(Event);
        }

        private void isUntil_Checked(object sender, RoutedEventArgs e)
        {
            until.IsEnabled = true;
            if (until.CurrentlySelectedDate < Event.Date) until.CurrentlySelectedDate = Event.Date;
            else
            {
                Event.Until = until.CurrentlySelectedDate;
                mainWindow.Data.Update(Event);
            }
        }

        private void until_SelectedDateChanged(object sender, AC.AvalonControlsLibrary.Controls.DateSelectedChangedRoutedEventArgs e)
        {
            if (until.CurrentlySelectedDate >= Event.Date)
            {
                Event.Until = until.CurrentlySelectedDate;
                mainWindow.Data.Update(Event);
            }
            else until.CurrentlySelectedDate = Event.Date;
        }

        private void reminder_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Event.Reminder = (int)reminder.SelectedValue;
            mainWindow.Data.Update(Event);
        }

        private void Expander_Expanded(object sender, RoutedEventArgs e)
        {
            DoubleAnimation da1 = new DoubleAnimation(1, new Duration(TimeSpan.FromSeconds(0.25)));
            DoubleAnimation da2 = new DoubleAnimation(border.Height + 138, new Duration(TimeSpan.FromSeconds(0.25)));

            details.BeginAnimation(Grid.OpacityProperty, da1);
            border.BeginAnimation(HeightProperty, da2);
        }

        private void Expander_Collapsed(object sender, RoutedEventArgs e)
        {
            DoubleAnimation da1 = new DoubleAnimation(0, new Duration(TimeSpan.FromSeconds(0.25)));
            DoubleAnimation da2 = new DoubleAnimation(border.Height - 138, new Duration(TimeSpan.FromSeconds(0.25)));

            details.BeginAnimation(Grid.OpacityProperty, da1);
            border.BeginAnimation(HeightProperty, da2);
        }
    }
}
